package com.xiran.srpingboottemplateself.common;


import com.xiran.srpingboottemplateself.exception.BaseExceptionInterface;
import com.xiran.srpingboottemplateself.exception.BizException;
import com.xiran.srpingboottemplateself.pojo.vo.LoginRspVO;
import lombok.Data;

import java.io.Serializable;

@Data
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    // 是否成功，默认为 true
    private boolean success = true;
    // 响应消息
    private String message;
    // 异常码
    private String errorCode;
    // 响应数据
    private T data;

    // =================================== 成功响应 ===================================
    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }

    // =================================== 失败响应 ===================================
    public static <T> Response<T> fail() {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }

    // 1. 生成失败的响应对象（保留原功能，方法名不变）
    public static <T> Response<T> fail(String errorMessage) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errorMessage);
        return response;
    }

    // 2. 返回失败的错误信息字符串（修改方法名，避免冲突）
    public static String getFailMessage(String errorMessage) {
        // 这里逻辑可以简化，直接返回传入的errorMessage（或根据需求处理）
        return errorMessage;
    }

    public static <T> Response<T> fail(String errorCode, String errorMessage) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(errorCode);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(BizException bizException) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        response.setMessage(baseExceptionInterface.getErrorMessage());
        return response;
    }


    public static Response fail(int i, String msg) {
        Response response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(String.valueOf(i));
        response.setMessage(msg);
        return response;
    }
}
